package cn.snowheart.command;

import cn.snowheart.command.command.Command;
import cn.snowheart.command.command.NoCommand;

/**
 * @author liuwanxiang
 * @version 2019/07/04
 */
public class RemoteControl {

    private int slotCount;
    private Command[] onCommands;
    private Command[] offCommands;

    public RemoteControl(int slotCount) {

        // 插槽入参校验
        if (slotCount < 1) {
            throw new IllegalArgumentException("Slot count not allowed less than 1!");
        }

        // 相关参数赋默认值
        this.slotCount = slotCount;
        this.onCommands = new Command[slotCount];
        this.offCommands = new Command[slotCount];
        NoCommand command = new NoCommand();

        for (int i = 0; i < slotCount; i++) {
            onCommands[i] = command;
            offCommands[i] = command;
        }

    }

    public void setCommand(int slot, Command onCommand, Command offCommand) {
        // 入参校验
        check(slot);
        onCommands[slot] = onCommand;
        offCommands[slot] = offCommand;
    }

    public void onPressed(int slot) {
        // 入参校验
        check(slot);
        onCommands[slot].execute();
    }

    public void offPressed(int slot) {
        // 入参校验
        check(slot);
        offCommands[slot].execute();
    }

    private void check(int slot) {
        if (slot < 0 || slot >= slotCount) {
            throw new IllegalArgumentException("Slot number not allowed less than 0 or bigger than slotCount!");
        }
    }
}
